<?php
namespace app\admin\controller;

use think\Request;

class User extends Common
{
    //轮播图首页
    public function index()
    {
        //升序排列
        $userlist = db('user')->order('id asc')->paginate(2);
        $page=$userlist->render();
        //dump();die;
        $row=[];
        for($i=0;$i<count($userlist);$i++){
            $row[]=$userlist[$i];
            if(empty($userlist[$i]['openid'])){
                $row[$i]['openid']= '否';
            }else{
                $row[$i]['openid']= '是';
            }
            if(empty($userlist[$i]['weibo_uid'])){
                $row[$i]['weibo_uid']= '否';
            }else{
                $row[$i]['weibo_uid']= '是';
            }
            if($userlist[$i]['sex']==0){
                $row[$i]['sex']= '未知';
            }else if($userlist[$i]['sex']==1){
                $row[$i]['sex']= '男';
            }else if($userlist[$i]['sex']==2){
                $row[$i]['sex']= '女';
            }
        };
        //dump($row);die;
        $page = str_replace(
            ["pagination", 
            "disabled",
            "active"],
        ["am-pagination tpl-pagination",
        "am-disabled",
        "am-active"],
        $page);
        //echo $page;die;
        $this->assign('userlist', $row);
        $this->assign('page', $page);
        return view();
    }

    //上传轮播图
    public function upimg(){

        $file = request()->file('headimg_url');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $img_src = '/uploads/'.$info->getSaveName();
                //echo $img_src;
                //制作缩略图
                //打开图片
                $image = \think\Image::open('.'.$img_src);
                //echo $image;die();
                $date =date('Ymd',time());
                $path="uploads/thumbs/$date";
                //echo $path;die;
                if(!file_exists($path)){
                    mkdir($path, 0777,true);
                }
                //保存缩略图 前面要加点
                $image->thumb(150,100)->save('.'.'/uploads/thumbs/'.$info->getSaveName());
                $thumb_src='/uploads/thumbs/'.$info->getSaveName();
                //echo $thumb_src;die;
                return ['headimg_url'=>$img_src,'thumb_headimg_url'=>$thumb_src];
                // return ['src'=>$img_src];
            }
        }
        else{
                $this->error($file->getError());
            }
    }

    //用户编辑页面
    public function edit()
    {
        $id=input('get.id');
        $result = db('user')->where("id={$id}")->find();
        //dump($result);die;
        $this->assign('result',$result);
        return view();
    }

    //改用户资料
    public function update()
    {
        $id=input('param.id');
        $data=input('post.');
        $result = db('user')->where("id={$id}")->find();
        //dump($result);dump($data);die;
        $validate = validate('User');
        $rule =['name'=>'require'];
        if(!empty($data['headimg_url'])){
            $rule[]='headimg_url';
        }
        //验证场景,验证想验证的
        $validate->scene('edit',$rule);
        $result = $validate->scene('edit')->check($data);
        if(!$result){
            $this->error( $validate->getError());
        }
        if(empty($data['headimg_url'])){
            unset($data['headimg_url']);
        }
        $ret=db('user')->where("id={$id}")->update($data);
        if($ret == false){
            $this->error('修改失败');
        }else{
            $this->success('修改成功');
        }
    }

    //删除用户
    public function delete(){
        $id=$_GET['id'];
        $ret=db('user')->where("id={$id}")->delete();
        if($ret){
            $this->success("删除成功!");
        }else{
            $this->error("删除失败");
        }
    }
}
